import BaseEntity from '@/framework/domain/BaseEntity'
import SpuPropertyValue from '@/feature/pdt/domain/SpuPropertyValue'

export default class SpuProperty extends BaseEntity {

  constructor(props, isSaleProp, skuList = [], isSelfProperty = false) {
    super(props)
    this.initChecked = false
    this.title = this.title || ''
    this.value = this.value || '' // 商品属性也需要
    this.deletable = this.deletable || false
    this.uploadPicFlag = this.uploadPicFlag || 0
    this.isUploadPic = this.isUploadPic || false
    this.selectable = this.selectable || isSelfProperty
    this.valueList = this.valueList && this.valueList.map(el => new SpuPropertyValue(el, false, false, this)) || []
    this.sourceValueList = this.valueList
    if (isSaleProp) {
      this.parseValueAsGroup(skuList)
      if (this.canSelfEdit || this.selectable || this.isDialogInput) {
        // 添加属性
        this.addPropertyValue(this.selectable)
      }
    }
  }

  clear() {
    this.valueList = []
    this.parseValueAsGroup([])
    // 添加属性
    this.addPropertyValue()
  }

  syncPropertyIdByTitle(options) {
    this.id = options.find(el => el.title === this.title).id
    Object.values(this.valueList).forEach(valueList => valueList.forEach(v => v.propertyId = this.id))
  }

  isEmpty() { // 属性是否为空
    return !this.title || !this.valueList[this.selectedGroup].length
  }

  get checkedValues() {
    return this.valueList[this.selectedGroup].filter(el => el.checked && el.value)
  }

  get hasEmptyProperty() {
    return this.valueList[this.selectedGroup].some(el => el.value === '')
  }

  assertPropertyValueNotExisted(v) {
    if (this.valueList[this.selectedGroup].some(el => el !== v && el.value === v.value)) {
      v.resumeValue && v.resumeValue()
      throw new Error('与其他规格名字重复，请修改')
    }
    v.storeValue && v.storeValue()
  }

  addPropertyValue(isSelfProperty) {
    if (!this.hasEmptyProperty) {
      Object.keys(this.valueList).forEach(key => {
        this.valueList[key].push(new SpuPropertyValue({
          value: '',
          // valueSource: isSelfProperty ? 1 : 0
          valueSource: 0 // 默认非自定义，blur事件会处理
        }, true, this.initChecked, this))
      })
    }
  }

  addPropertyValueForCurrent() {
    if (!this.hasEmptyProperty) {
      this.valueList[this.selectedGroup].push(new SpuPropertyValue({
        value: '',
        valueSource: 0 // 默认非自定义，blur事件会处理
      }, true, this.initChecked, this))
    }
  }

  removeExtraEmptyProperty(v) {
    Object.values(this.valueList).forEach(valueList => {
      const filterValueList = valueList.filter(el => el.value === '')
      if (filterValueList.length > 1) {
        const pos = valueList.findIndex(el => el === v)
        if (pos > -1) {
          valueList.splice(pos, 1)
        } else {
          valueList.pop()
        }
      }
    })
    v.storeValue()
  }

  removeProperty(v) {
    Object.values(this.valueList).forEach(valueList => {
      const idx = valueList.findIndex(el => el === v)
      if (idx > -1) {
        valueList.splice(idx, 1)
      }
    })
  }

  get canSelfEdit() {
    // todo
    return this.feature === 2 || this.feature === 0
  }

  parseValueAsGroup(skuList) {
    if (this.isDialogInput) {
      this.groupList = []
      this.valueList = {'': this.spuValues.map(el => new SpuPropertyValue(el, false, true, this))}
      this.selectedGroup = ''
      this.initChecked = true // 初始选中
      return
    }
    if (this.valueList.some(el => el.group)) {
      const groupMap = {}
      this.valueList = this.valueList.reduce((r, a) => {
        const key = a.group && a.group.id || '0'
        groupMap[key] = a.group || null
        r[key] = r[key] || []
        // a.id
        r[key].push(a)
        return r
      }, {})
      const selfSpuValues = this.spuValues && this.spuValues.filter(el => el.valueSource === 1) || [] // 自定义属性值
      if (selfSpuValues.length > 0) {
        this.isUploadPic = selfSpuValues.some(el => el.previewImg)
        selfSpuValues.forEach(v => {
          this.valueList[v.groupId].push(new SpuPropertyValue(v, true, true, this))
        })
      }
      this.groupList = Object.values(groupMap).filter(el => el)
      if (this.groupList.length > 0 && skuList && skuList.length) {
        const salePropValueIds = skuList[0].salePropValueIds.split(',')
        const group = Object.values(this.valueList).find(el => el.some(i => salePropValueIds.includes(i.id)))
        if (group) {
          this.selectedGroup = group[0].groupId
        }
      }
      if (!this.selectedGroup) {
        this.selectedGroup = this.groupList.length > 0 ? this.groupList[0].id : '0'
      }
    } else {
      const selfSpuValues = this.spuValues && this.spuValues.filter(el => el.valueSource === 1) || [] // 自定义属性值
      if (selfSpuValues.length > 0) {
        this.isUploadPic = selfSpuValues.some(el => el.previewImg)
        selfSpuValues.forEach(v => {
          this.valueList.push(new SpuPropertyValue(v, true, true, this))
        })
      }
      this.groupList = []
      this.valueList = {'0': this.valueList}
      this.selectedGroup = '0'
    }
    // sort
    Object.keys(this.valueList).forEach(key => {
      this.valueList[key] = this.valueList[key].sort((a, b) => a.value > b.value ? 1 : -1)
    })
  }

  get isValueGrouped() {
    return this.groupList && this.groupList.length > 0
  }

  get isRequired() {
    return this.requiredFlag === 1
  }

  get isImportant() {
    return this.importantFlag === 1
  }

  // bath setting
  get isNotEmpty() {
    return !!this.value
  }

  get isSelectInput() {
    // return this.selectType === 2 || this.selectType === 0
    return this.feature === 2 || this.feature === 0
  }

  get isDialogInput() {
    return this.feature === 1
  }

  get canSelfAdd() {
    return this.selectType === 2
  }

  getSpuProp() {
    if (!this.value) {
      return null
    }
    return {
      propertyValue: this.value,
      propertyId: this.id
    }
  }
}
